package com.zyp.messageboard.controller;

import com.zyp.messageboard.entity.User;
import com.zyp.messageboard.service.UserService;
import com.zyp.messageboard.service.impl.UserServiceImpl;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    private final UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if (session != null) {
            User user = (User) session.getAttribute("user");
            if (user != null) {
                // 更新用户在线状态为离线
                userService.updateOnlineStatus(user.getId(), false);
            }
            session.invalidate();
        }
        response.sendRedirect("login.jsp");
    }
}
